﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.BidSystem.Model;

public partial class Manage_ThreePwdSetting : System.Web.UI.Page, IAccess
{
    protected string gvshow = "inline";


    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            // 判断是否设置了三方密码
            var list = BLL_Factory.IBLL_Manage.GetThreePwdConfig(CurrentAccount.ProjectId);

            if (list.Count > 0) // 设置了 绑定数值
            {

                txtDecCount.Text = list.Count.ToString();
                txtDecCount.ReadOnly = true;
                btnSetDecCount.Visible = false;

                BindData();
            }
            else
            {
                btnSetDecCount.Visible = true;
                gvshow = "none";
            }

        }
    }

    private void BindData()
    {

        var list = BLL_Factory.IBLL_Manage.GetThreePwdConfig(CurrentAccount.ProjectId);
        gvdecpwdsetting.DataSource = list;
        gvdecpwdsetting.DataBind();
    }


    protected void gvdecpwdsetting_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
            var dataitem = e.Row.DataItem as ThreePwdConfig;

            var txtPwd1 = e.Row.FindControl("txtPwd1") as TextBox;
            var txtPwd2 = e.Row.FindControl("txtPwd2") as TextBox;
            var txtCompeteDecName = e.Row.FindControl("txtCompeteDecName") as TextBox;
            var lkbSave = e.Row.FindControl("lkbSave") as LinkButton;
            if (!string.IsNullOrEmpty(dataitem.CompeteDecPassword)) // 设置了密码
            {
               

                lkbSave.Visible = false;
                txtCompeteDecName.ReadOnly = true;
                txtPwd1.Visible = false;
                txtPwd2.Visible = false;
            }
            else
            {

            }

        }
    }
    protected void btnSetDecCount_Click(object sender, EventArgs e)
    {
        int? i = HZ.Common.HzConvert.ToInt32(txtDecCount.Text);
        if (i == null)
        {
            HZ.Web.WebUtil.Alert("输入不符合数字", this);
            return;
        }
        else
        {
            i = BLL_Factory.IBLL_Manage.SetThreePwdConfigCount(CurrentAccount.ProjectId, i.Value);
            txtDecCount.ReadOnly = true;
            btnSetDecCount.Visible = false;
            gvshow = "inline";
            BindData();
        }
    }
    protected void lkbSave_Click(object sender, EventArgs e)
    {

        var lkbSave = sender as LinkButton;
        int id = HZ.Common.HzConvert.ToInt32(lkbSave.CommandArgument).Value;
        foreach (GridViewRow item in gvdecpwdsetting.Rows)
        {
            if (item.Cells[0].Text == id.ToString())
            {
                var txtPwd1 = item.FindControl("txtPwd1") as TextBox;
                var txtPwd2 = item.FindControl("txtPwd2") as TextBox;
                var txtCompeteDecName = item.FindControl("txtCompeteDecName") as TextBox;
                if (string.IsNullOrEmpty(txtPwd1.Text))
                {
                    HZ.Web.WebUtil.Alert("密码不能空", this);

                    return;
                }
                if (txtPwd1.Text != txtPwd2.Text)
                {
                    HZ.Web.WebUtil.Alert("前后密码不一致", this);

                    return;
                }

                if (string.IsNullOrEmpty(txtCompeteDecName.Text))
                {
                    HZ.Web.WebUtil.Alert("机构名不能空", this);

                    return;
                }

                BLL_Factory.IBLL_Manage.SetThreePwdConfig(new ThreePwdConfig() { ProjectId = CurrentAccount.ProjectId, CompeteDecId = id, CompeteDecName = txtCompeteDecName.Text, CompeteDecPassword = HZ.Fun.GetMd5(txtPwd1.Text) });

                HZ.Web.WebUtil.Alert("设置成功", this);
                break;
            }
        }
        BindData();
    }



    #region IAccess Members

    IList<EnumAccess> IAccess.AccessList
    {
        get
        {
            var list = new List<EnumAccess>();
            list.Add(EnumAccess.Manage);
            return list;
        }

    }

    #endregion
}
